package com.wz.project.mydo.mydojob.controller;

import com.wz.common.utils.poi.ExcelUtil;
import com.wz.framework.aspectj.lang.annotation.Log;
import com.wz.framework.aspectj.lang.enums.BusinessType;
import com.wz.framework.web.controller.BaseController;
import com.wz.framework.web.domain.AjaxResult;
import com.wz.framework.web.page.TableDataInfo;
import com.wz.project.mydo.domain.MydoJob;
import com.wz.project.mydo.domain.MydoJobFinish;
import com.wz.project.mydo.mydojob.service.IJobFinishJService;
import com.wz.project.mydo.mydojob.service.IMydoJobService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 作业表Controller
 * 
 * @author wz
 * @date 2021-11-02
 */
@Controller
@RequestMapping("/out/mydojob")
public class OutMydoJobController extends BaseController
{
    private String prefix = "mydo/mydojob";

    @Autowired
    private IMydoJobService jobService;
    @Autowired
    private IJobFinishJService finishService;
//    @RequiresPermissions("out:mydojob:view")
    @GetMapping()
    public String mydojob()
    {
        return prefix + "/outmydojob";
    }

    /**
     * 查询作业表列表
     */
//    @RequiresPermissions("out:mydojob:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(MydoJob mydoJob, MydoJobFinish jobFinish,String inOut)
    {
        mydoJob.setDevId(inOut);
        jobFinish.setDevId(inOut);
        if ("4".equals(mydoJob.getStatus()) || "5".equals(mydoJob.getStatus())){
            startPage();
            List<MydoJobFinish> list = finishService.selectJobFinishList(jobFinish);
            return getDataTable(list);
        }else{
            startPage();
            List<MydoJob> list = jobService.selectJobList(mydoJob);
            return getDataTable(list);
        }
    }

    /**
     * 新增作业表
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存作业表
     */
    @RequiresPermissions("out:mydojob:add")
    @Log(title = "作业表", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(MydoJob mydoJob)
    {
        return toAjax(jobService.insertJob(mydoJob));
    }

    /**
     * 修改作业表
     */
    @GetMapping("/edit/{jobId}")
    public String edit(@PathVariable("jobId") String jobId, ModelMap mmap)
    {
        MydoJob mydoJob = jobService.selectJobById(jobId);
        mmap.put("mydoJob", mydoJob);
        return prefix + "/edit";
    }

    /**
     * 修改保存作业表
     */
    @RequiresPermissions("out:mydojob:edit")
    @Log(title = "作业表", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(MydoJob mydoJob)
    {
        return toAjax(jobService.updateJob(mydoJob));
    }

    /**
     * 删除作业表
     */
    @RequiresPermissions("out:mydojob:remove")
    @Log(title = "作业表", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(jobService.deleteJobByIds(ids));
    }

    /**
     * 手动作废
     */
    @RequestMapping("/handInvalid")
    @ResponseBody
    public AjaxResult handInvalid(String jobId){

        int i = jobService.handInvalid(jobId);
        if (i == 1) {
            return AjaxResult.error("作业不存在,请核对！");
        }else if (i == 2) {
            return AjaxResult.error("只有未执行作业才可作废！");
        } else if (i == 10) {
            return AjaxResult.success( "作废成功！");
        } else  {
            return AjaxResult.error("操作失败，请重试！");
        }

    }
    /**
     * 手动完成
     */
    @RequestMapping("/handFinish")
    @ResponseBody
    public AjaxResult handFinish(String jobId){
        return jobService.handFinishOut(jobId);
    }
    //	@RequiresPermissions("mydo:mydojob:changeJobStatus")
    @RequestMapping("/changeJobpriority")
    @ResponseBody
    public AjaxResult changeJobpriority(String ids,int pro ){
        String id[] = ids.split(",");
        int i = jobService.changeJobpriority(id,pro);
        if (i == 1){
            return AjaxResult.success("更改状态成功");
        }else{
            return AjaxResult.error("只允许修改未执行的作业优先级");
        }
    }
}
